package com.example.yoho.mvp.view.fragment;



import android.support.v4.app.Fragment;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;

import com.example.lib_core.mvp.view.BaseFragment;
import com.example.yoho.R;
import com.example.yoho.di.component.DaggerCommunityComponent;
import com.example.yoho.di.moudles.CommunityModules;
import com.example.yoho.domain.Domain_Url;
import com.example.yoho.mvp.contract.CommunityContract;
import com.example.yoho.mvp.model.entity.CommunityBean;
import com.example.yoho.mvp.presenter.CommunityPresenter;
import com.example.yoho.mvp.view.adapter.CommunityAdapters;
import com.example.yoho.utils.MyGlide;
import com.jwenfeng.library.pulltorefresh.BaseRefreshListener;
import com.jwenfeng.library.pulltorefresh.PullToRefreshLayout;
import com.jwenfeng.library.pulltorefresh.view.CategoryHeadRefreshView;

import java.util.ArrayList;
import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class MyCommunityFragment extends BaseFragment<CommunityPresenter> implements CommunityContract.View {


    private CommunityAdapters adapters;
    private List<CommunityBean.ValuesBean> list=new ArrayList<>();
    private com.jwenfeng.library.pulltorefresh.PullToRefreshLayout mycommunityPull;
    private android.support.v7.widget.RecyclerView mycommunityRecyc;
    private ImageView imageView;
    private View view;

    @Override
    public int bondLayout() {
        return R.layout.fragment_my_community;
    }

    @Override
    public void Inject() {
        DaggerCommunityComponent.builder().communityModules(new CommunityModules(this)).build().Inject(this);
    }

    @Override
    public void initView() {

        mycommunityPull = (PullToRefreshLayout) findviewByIds(R.id.mycommunity_pull);
        mycommunityRecyc = (RecyclerView) findviewByIds(R.id.mycommunity_recyc);
         view = LayoutInflater.from(getContext()).inflate(R.layout.mycommunity_headview, null);
         imageView=view.findViewById(R.id.communityhead_image);
         mycommunityPull.setHeaderView(new CategoryHeadRefreshView(getContext()));
        mycommunitypulls();

    }

    private void mycommunitypulls() {
        mycommunityPull.setRefreshListener(new BaseRefreshListener() {
            @Override
            public void refresh() {
                list.clear();
                adapters.notifyDataSetChanged();
                mPresenter.reuqestCommunityAll();
            }

            @Override
            public void loadMore() {
               mPresenter.reuqestCommunityAll();

            }
        });
    }

    @Override
    public void initData() {
        mPresenter.reuqestCommunityAll();
        adapters=new CommunityAdapters(R.layout.community,list);
        mycommunityRecyc.setAdapter(adapters);
        mycommunityRecyc.setLayoutManager(new LinearLayoutManager(getContext()));
        adapters.addHeaderView(view);
    }

    @Override
    public void showCommunity(CommunityBean communityBean) {
        Log.e("aaaa", "showCommunity: "+communityBean.getValues().size() );
        List<CommunityBean.ValuesBean> values = communityBean.getValues();
        list.addAll(values);
        adapters.notifyDataSetChanged();
        MyGlide.GlideFor(getContext(), Domain_Url.IMAGE_URL+communityBean.getBanner().get(0).getRecommend_url(),imageView);
        mycommunityPull.finishRefresh();
        mycommunityPull.finishLoadMore();
    }
}
